// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_INV_IO
// * CreateDate    : 2013-12-20 23:31:43
// * Creater       : CodeSmith
// * LastChangeDate: 2013-12-20 23:31:43
// * LastChanger   : 2013-12-20 23:31:43
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Inv
{
/// <summary>
    /// InvIo
    /// </summary>
    public partial class InvIoBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objInvIoEntity">Entity</param>
        /// <returns>Entity</returns>
        public static InvIoEntity GetInvIo(InvIoEntity objInvIoEntity)
        {
            return DataAccess.SelectSingle<InvIoEntity>(objInvIoEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objInvIoEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertInvIo(InvIoEntity objInvIoEntity)
        {
            return DataAccess.Insert(objInvIoEntity);
        }

        public static bool InsertInvIo(List<InvIoEntity> objInvIoEntityList)
        {
            return DataAccess.Insert(objInvIoEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objInvIoEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateInvIo(InvIoEntity objInvIoEntity)
        {
            return DataAccess.Update(objInvIoEntity);
        }

        public static bool UpdateInvIo(List<InvIoEntity> objInvIoEntityList)
        {
            return DataAccess.Update<InvIoEntity>(objInvIoEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objInvIoEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteInvIo(InvIoEntity objInvIoEntity)
        {
            return DataAccess.Delete(objInvIoEntity);
        }

        public static bool DeleteInvIo(List<InvIoEntity> objInvIoEntityList)
        {
            return DataAccess.Delete<InvIoEntity>(objInvIoEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetInvIoAll()
        {
            return DataAccess.SelectAllDataSet<InvIoEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<InvIoEntity> GetInvIoAllList()
        {
            return DataAccess.SelectAll<InvIoEntity>();
        }
        #endregion

        public static DataSet GetAllDsBySql(string sql)
        {
            return DataAccess.SelectDataSet(sql);
        }

        public static bool AddInvIo(InvIoEntity io,List<InvIoiEntity> ioi)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert(io, broker);
                    DataAccess.Insert(ioi, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static DataSet GetAllDsByCondition(int pageIndex, int pageCount, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from INV_IO) as cnt
from INV_IO
where id not in (
    select top ({1}*({2}-1)) id from INV_IO order by created_date
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex);
                sql = @"select io.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) io
left join BPMDB.dbo.PW_User u1 on io.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on io.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date
) order by created_date desc";
                string sqlSource = @"select io.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from INV_IO io
left join BPMDB.dbo.PW_User u1 on io.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on io.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);
        }

	}
	
	
}	

